﻿using QLBH.Models;
using QLBH.Utilities;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace QLBH.Modules
{
    public partial class frmAdd_Edit_Provider : Form
    {
        private eType eType = eType.ADD;
        private ProviderModel model;
        public ProviderModel Model
        {
            get { return model; }
            set { model = value; }
        }


        public frmAdd_Edit_Provider(eType eType)
        {
            InitializeComponent();
            this.eType = eType;
            model = new ProviderModel();
        }

        private void frmAdd_Edit_Provider_Load(object sender, EventArgs e)
        {
            if (this.eType == eType.ADD)
            {
                this.Text = "Thêm Nhà Cung Cấp";
            }
            else
            {
                this.Text = "Sửa Nhà Cung Cấp";
                //load data griview to textbox
                txtName.Text = Model.Name;
                rtbAddress.Text = Model.Address;
                txtPhone.Text = Model.Phone;
                txtWebsite.Text = Model.Website;
                txtEmail.Text = Model.Email;
                rtbDescription.Text = Model.Comments;
            }
        }
        private bool CheckValidData()
        {
            try
            {
                DataInputHelper.CheckTextData(txtName.Text, "Tên Nhà Cung Cấp", 30);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message, "Quản Lý Nhà Cung Cấp", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtName.SelectAll();
                txtName.Focus();
                return false;
            }
            try
            {
                DataInputHelper.CheckTextData(rtbAddress.Text, "Địa Chỉ", 200);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message, "Quản Lý Nhà Cung Cấp", MessageBoxButtons.OK, MessageBoxIcon.Error);
                rtbAddress.SelectAll();
                rtbAddress.Focus();
                return false;
            }
            try
            {
                DataInputHelper.CheckTextData(txtPhone.Text, "Số Điện Thoại", 12);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message, "Quản Lý Nhà Cung Cấp", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtPhone.SelectAll();
                txtPhone.Focus();
                return false;
            }
            try
            {
                DataInputHelper.CheckPhoneNumber(txtPhone.Text);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message, "Quản Lý Nhà Cung Cấp", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtPhone.SelectAll();
                txtPhone.Focus();
                return false;
            }
            try
            {
                DataInputHelper.CheckTextData(txtEmail.Text, "Email", 50);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message, "Quản Lý Nhà Cung Cấp", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtEmail.SelectAll();
                txtEmail.Focus();
                return false;
            }
            try
            {
                DataInputHelper.CheckEmailData(txtEmail.Text);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message, "Quản Lý Nhà Cung Cấp", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtEmail.SelectAll();
                txtEmail.Focus();
                return false;
            }
            try
            {
                DataInputHelper.CheckTextData(txtWebsite.Text, "Website", 30);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message, "Quản Lý Nhà Cung Cấp", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtWebsite.SelectAll();
                txtWebsite.Focus();
                return false;
            }
            try
            {
                DataInputHelper.CheckTextData(rtbDescription.Text, "Ghi Chu", 200);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message, "Quản Lý Nhà Cung Cấp", MessageBoxButtons.OK, MessageBoxIcon.Error);
                rtbDescription.SelectAll();
                rtbDescription.Focus();
                return false;
            }
            return true;
        }
        private void btnLuu_Click(object sender, EventArgs e)
        {
            if (CheckValidData())
            {
                Model.Name = txtName.Text;
                Model.Address = rtbAddress.Text;
                Model.Phone = txtPhone.Text;
                Model.Website = txtWebsite.Text;
                Model.Email = txtEmail.Text;
                Model.Comments = rtbDescription.Text;
                //check dieu kien truoc khi chay
                if (eType == eType.ADD)
                {
                    try
                    {

                        ProviderInsertResponse result = WSAccess.Insert<ProviderModel, ProviderInsertResponse>(model, MainForm.Instance.Shop_id + "/provider/insert");
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
                else
                {
                    try
                    {
                        WSAccess.Update<ProviderModel>(model, Model.Id + "/provider/edit");
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
            else
            {
                this.DialogResult = DialogResult.None;
            }
        }
    }
    public class ProviderInsertResponse : ResponseBase
    {
        private int provider_id;

        public int Provider_id
        {
            get { return provider_id; }
            set { provider_id = value; }
        }

    }
}
